﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace DockSample
{
    public class ObjectModelForm : DummyModelForm
    {
        //parentOrderObjectDiagram不需要存数据库
        public HighOrderObjectDiagram parentOrderObjectDiagram = null;
        
        public ObjectModelForm()
            :base()
        {
            InitializeComponent();
            initContextMenuStrip();
            SystemVar.mainForm.modelFormList.Add(this);
            SystemVar.mainForm.SolutionExplorer.updateSolutionExplorerTreeView();
        }

        private void InitializeComponent()
        {
            
        }
        //
        protected override void DummyModelForm_Click(object sender, EventArgs e)
        {
            SystemVar.setSelectableToolBox(this);
            base.DummyModelForm_Click(sender, e);
        }
        //
        protected override void DummyModelForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            base.DummyModelForm_FormClosing(sender, e);
        }

        private void ObjectModelForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if(SystemVar.currentDiagramFlag.Equals(Diagram.HighOrderObject))
            {
                Point pt = new Point(e.X, e.Y);
                this.diagramList.Add(new HighOrderObjectDiagram(pt));
                Invalidate();
            }
        }

        protected override void DummyModelForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {            
            #region 左键双击
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                #region 双击弹出对话框
                //双击弹出对话框或进入下层设计
                foreach (OriginalDiagram diag in diagramList)
                {
                    //当图形对象为HighOrderObjectDiagram类的对象
                    if (diag is HighOrderObjectDiagram)
                    {
                        HighOrderObjectDiagram obj = (HighOrderObjectDiagram)diag;
                        Rectangle rect = new Rectangle(obj.UpLeftpoint.X, obj.UpLeftpoint.Y, obj.Width / 2, obj.Height);
                        Rectangle rect1 = new Rectangle(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + 2 * diag.Height / 21, diag.Width / 2, 5 * diag.Height / 21);
                        Rectangle rect2 = new Rectangle(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + 8 * diag.Height / 21, diag.Width / 2, 5 * diag.Height / 21);
                        Rectangle rect3 = new Rectangle(diag.UpLeftpoint.X + diag.Width / 2, diag.UpLeftpoint.Y + 14 * diag.Height / 21, diag.Width / 2, 5 * diag.Height / 21);
                        Point pt = new Point(e.X, e.Y);
                        
                        //处理显示平台
                        if (rect.Contains(pt))
                        {
                            //图形对象当前没有下层分解平台，构造新MainForm，以无模式对话框形式弹出
                            if (!obj.HasModelForm)
                            {
                                ObjectModelForm modelForm = SystemVar.mainForm.createNewObjectModelForm("ObjectModelForm/ " + obj.Name);
                                
                                //新建的Form属于obj对象
                                obj.NewModelForm = modelForm;
                                obj.HasModelForm = true;
                                obj.newModelFormId = modelForm.id;
                                //modelForm.parentOrderObjectDiagram = obj;
                                //添加外部接口对象
                                addExternalInterface(obj, modelForm);
                                //同步Solution Explorer
                                //SystemVar.mainForm.modelFormList.Add(this);
                                SystemVar.mainForm.SolutionExplorer.updateSolutionExplorerTreeView();
                            }
                            //图形对象当前有下层分解平台，原有的非模式窗口获得焦点
                            else
                            {
                                obj.NewModelForm.Focus();
                            }
                            break;
                        }
                        //处理方法
                        else
                        {
                            //根据双击坐标弹出对话框
                            if (rect1.Contains(pt))
                            {
                                showSpecificationDialogandUpdataDiagramContent(obj, "attributesTabControl");
                                break;
                            }
                            else if (rect2.Contains(pt))
                            {
                                showSpecificationDialogandUpdataDiagramContent(obj, "methodsTabControl");
                                break;
                            }
                            else if (rect3.Contains(pt))
                            {
                                showSpecificationDialogandUpdataDiagramContent(obj, "NFRTabControl");
                                break;
                            }
                            //更新图形对象数据
                        }
                        
                    }
                    //当图形对象为PrimitiveObjectDiagram类的对象
                    else if (diag is PrimitiveObjectDiagram)
                    {
                        PrimitiveObjectDiagram obj = (PrimitiveObjectDiagram)diag;
                        Rectangle rect1 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y, diag.Width, diag.Height / 3);
                        Rectangle rect2 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y + diag.Height / 3, diag.Width, diag.Height / 3);
                        Rectangle rect3 = new Rectangle(diag.UpLeftpoint.X, diag.UpLeftpoint.Y + 2 * diag.Height / 3, diag.Width, diag.Height / 3);
                        Point pt = new Point(e.X, e.Y);

                        if (rect1.Contains(pt))
                        {
                            showSpecificationDialogandUpdataDiagramContent(obj, "attributesTabControl");
                            break;
                        }
                        else if (rect2.Contains(pt))
                        {
                            showSpecificationDialogandUpdataDiagramContent(obj, "methodsTabControl");
                            break;
                        }
                        else if (rect3.Contains(pt))
                        {
                            showSpecificationDialogandUpdataDiagramContent(obj, "NFRTabControl");
                            break;
                        }
                    }
                }
                #endregion
            }
            #endregion

            #region 鼠标左击
            else //if (e.Button == MouseButtons.Left)
            {
                ClearSelection();
                Point pt = new Point(e.X, e.Y);
                //添加图形对象
                if (SystemVar.currentDiagramFlag == Diagram.HighOrderObject || SystemVar.currentDiagramFlag == Diagram.PrimitiveObject
                    || SystemVar.currentDiagramFlag == Diagram.ExternalInterfaceIn || SystemVar.currentDiagramFlag == Diagram.ExternalInterfaceOut)
                {
                    AddDiagram(SystemVar.currentDiagramFlag, pt);
                }
                //画线准备
                else if ((SystemVar.currentDiagramFlag == Diagram.SimpleRelationship || SystemVar.currentDiagramFlag == Diagram.ComplexRelationship))
                {
                    DrawingLinePre(pt);
                }
                //选中操作
                else if (SystemVar.currentDiagramFlag == Diagram.NullObject)
                {
                    SelectDiagram(pt);
                    SelecteLine(pt);
                }
                //缩放准备
                if (preSelectedDiagram != null)
                {
                    selectedMagnifyingDiagramPoint(pt);
                }
                
            }
            #endregion

            #region 鼠标右击
            if (e.Button == MouseButtons.Right)
            {
                ShowContextMenuStrip(new Point(e.X, e.Y));
            }
            #endregion

            #region 文字同步
            //使所有对象的NameBox失去焦点
            //SystemVar.mainForm.SolutionExplorer.getTreeView().Focus();
            //RenameProcess();
            #endregion
            base.DummyModelForm_MouseDown(sender, e);
        }
        protected override void DummyModelForm_MouseEnter(object sender, EventArgs e)
        {
            switch (SystemVar.currentDiagramFlag)
            {
                case Diagram.HighOrderObject:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.PrimitiveObject:
                    Cursor = System.Windows.Forms.Cursors.Cross;
                    break;
                case Diagram.SimpleRelationship:
                    Cursor = System.Windows.Forms.Cursors.UpArrow;
                    break;
                case Diagram.ComplexRelationship:
                    Cursor = System.Windows.Forms.Cursors.UpArrow;
                    break;
                case Diagram.NullObject:
                    Cursor = System.Windows.Forms.Cursors.Default;
                    break;
            }
        }
        //
        protected override void DummyModelForm_MouseMove(object sender, MouseEventArgs e)
        {
            //移动
            Point pt = new Point(e.X, e.Y);
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovingDiagram(selectedDiagram, pt);
            }
            //缩放
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifyingDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawingLine(pt);
            }
        }
        //
        protected override void DummyModelForm_MouseUp(object sender, MouseEventArgs e)
        {
            Point pt = new Point(e.X, e.Y);
            //移动
            if (selectedDiagram != null && movingDiagramFlag)
            {
                MovedDiagram(selectedDiagram, pt);
            }
            //缩放
            else if (preSelectedDiagram != null && magnifyingDiagramFlag)
            {
                magnifiedDiagram(pt);
            }
            //画线
            else if (lineStartDiagram != null && drawingLineFlag)
            {
                DrawedLine(SystemVar.currentDiagramFlag, pt);
            }
            base.DummyModelForm_MouseUp(sender, e);
        }
        //
        //
        private void showSpecificationDialogandUpdataDiagramContent(ObjectDiagram obj, string tabDialog)
        {
            ObjectSpecificationDialog objectSpecification = initSpecificationDialog(obj);
            objectSpecification.setShowTabControl(tabDialog);
            if (objectSpecification.ShowDialog() == DialogResult.OK)
            {
                updataDiagramContent(obj, objectSpecification);
            }
            Invalidate();
        }
        //
        private ObjectSpecificationDialog initSpecificationDialog(ObjectDiagram obj)
        {
            //初始化对话框
            ObjectSpecificationDialog objectSpecification = new ObjectSpecificationDialog();
            objectSpecification.ObjectName = obj.Name;
            objectSpecification.Type = obj.Type;
            objectSpecification.StereoType = obj.Stereotype;
            objectSpecification.ExportControl = obj.ExportControl;
            objectSpecification.Documentation = obj.Documengtation;
            if (this.parentOrderObjectDiagram == null)
            {
                //查找此form对应的HighOrderObjectDiagram
                foreach (DummyModelForm modelForm in SystemVar.mainForm.modelFormList)
                {
                    foreach (OriginalDiagram diagram in modelForm.diagramList)
                    {
                        if (diagram is HighOrderObjectDiagram && ((HighOrderObjectDiagram)diagram).newModelFormId == this.id)//相与短路
                        {
                            this.parentOrderObjectDiagram = (HighOrderObjectDiagram)diagram;
                            break;//如何跳出两层循环？？？
                        }
                    }
                }
            }
            objectSpecification.ParentOrderObjectDiagram = this.parentOrderObjectDiagram;
            //填充NFR窗口内容
            StringBuilder NFRBuilder = new StringBuilder();//NFR
            if (obj.ObjectNFRList.Count != 0)
            {
                foreach (NonFunctionRequirment requirment in obj.ObjectNFRList)
                {
                    objectSpecification.TemporaryObjectNFRList.Add(requirment);
                }
                if (obj.ObjectNFRList.Count != 0)
                {
                    NFRBuilder.AppendLine(obj.Name + ":[" + getNFRListString(obj.ObjectNFRList) + "]");//NFR
                }
            }
            //填充methods窗口内容
            StringBuilder methodsBuilder = new StringBuilder();
            foreach (Method method in obj.MethodList)
            {
                methodsBuilder.AppendLine(method.ToString());
                objectSpecification.TemporaryMethodList.Add(method);//临时列表同步对象方法列表
                if (method.MethodNFRList.Count != 0)
                {
                    NFRBuilder.AppendLine(method.MethodName + ":[" + getNFRListString(method.MethodNFRList) + "]");//NFR
                }
            }
            //更新方法对话框显示
            objectSpecification.MethodShowBox.Text = methodsBuilder.ToString();

            //填充attributes窗口内容
            StringBuilder attributesBuilder = new StringBuilder();
            foreach (Attribute attr in obj.AttributeList)
            {
                attributesBuilder.AppendLine(attr.ToString());
                objectSpecification.TemporaryAttributeList.Add(attr);
                if (attr.AttributeNFRList.Count != 0)
                {
                    NFRBuilder.AppendLine(attr.AttributeName + ":[" + getNFRListString(attr.AttributeNFRList) + "]");//NFR
                }
            }
            objectSpecification.AttributeShowBox.Text = attributesBuilder.ToString();

            objectSpecification.NonFunctionShowBox.Text = NFRBuilder.ToString();//NFR
            //填充Relations窗口内容
            {
                StringBuilder relationBuilder = new StringBuilder();
                foreach (OriginalLine line in straightlineList)
                {
                    if (line.StartDiagram.Equals(obj))
                    {
                        relationBuilder.AppendLine(line.EndDiagram.Name);
                    }
                    else if (line.EndDiagram.Equals(obj))
                    {
                        relationBuilder.AppendLine(line.StartDiagram.Name);
                    }
                }
                objectSpecification.RelationsShowBox.Text = relationBuilder.ToString();
            }
            //
            return objectSpecification;
        }

        private string getNFRListString(List<NonFunctionRequirment> NFRList)
        {
            StringBuilder NFRBuilder = new StringBuilder();
            foreach (NonFunctionRequirment requirment in NFRList)
            {
                NFRBuilder.Append(requirment.ToString());
            }
            return NFRBuilder.ToString();
        }
        //
        private void updataDiagramContent(ObjectDiagram obj, ObjectSpecificationDialog objectSpecification)
        {
            obj.MethodList.Clear();//清空原有方法链表
            foreach (Method method in objectSpecification.TemporaryMethodList)
            {
                obj.MethodList.Add(method);//更新方法链表
            }
            //更新属性
            obj.AttributeList.Clear();
            foreach (Attribute attr in objectSpecification.TemporaryAttributeList)
            {
                obj.AttributeList.Add(attr);
            }
            //更新约束
            obj.ObjectNFRList.Clear();
            foreach (NonFunctionRequirment requirment in objectSpecification.TemporaryObjectNFRList)
            {
                obj.ObjectNFRList.Add(requirment);
            }
            //更新Detail
            obj.Name = objectSpecification.ObjectName;
            obj.NameBox.Text = obj.Name;
            obj.Type = objectSpecification.Type;
            obj.Stereotype = objectSpecification.StereoType;
            obj.ExportControl = objectSpecification.ExportControl;
            obj.Documengtation = objectSpecification.Documentation;
        }

        private void initContextMenuStrip()
        {
            modelFormContextMenuStrip.Items[4].Visible = false;
            modelFormContextMenuStrip.Items[5].Visible = false;
            modelFormContextMenuStrip.Items[6].Visible = false;
            modelFormContextMenuStrip.Items[7].Visible = false;
            modelFormContextMenuStrip.Items[8].Visible = false;
            modelFormContextMenuStrip.Items[9].Visible = false;
            modelFormContextMenuStrip.Items[11].Visible = false;

            diagramMenuStrip.Items[6].Visible = false;
        }
    }
}
